home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MultiUIDefaults.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  169 lines

  1. /*
  2.  * @(#)MultiUIDefaults.java    1.6 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.util.Enumeration;
  24.  
  25.  
  26.  
  27. /**
  28.  * 
  29.  * @version 1.6 02/02/98
  30.  * @author Hans Muller
  31.  */
  32. class MultiUIDefaults extends UIDefaults
  33. {
  34.     private UIDefaults[] tables;
  35.  
  36.     public MultiUIDefaults(UIDefaults[] defaults) {
  37.     super();
  38.     tables = defaults;
  39.     }
  40.  
  41.     public MultiUIDefaults() {
  42.     super();
  43.     tables = new UIDefaults[0];
  44.     }
  45.  
  46.  
  47.     public Object get(Object key) 
  48.     {
  49.     Object value = super.get(key);
  50.     if (value != null) {
  51.         return value;
  52.     }
  53.  
  54.     for(int i = 0; i < tables.length; i++) {
  55.         UIDefaults table = tables[i];
  56.         value = (table != null) ? table.get(key) : null;
  57.         if (value != null) {
  58.         return value;
  59.         }
  60.     }
  61.  
  62.     return null;
  63.     }
  64.  
  65.  
  66.     public int size() {
  67.     int n = super.size();
  68.     for(int i = 0; i < tables.length; i++) {
  69.         UIDefaults table = tables[i];
  70.         n += (table != null) ? table.size() : 0;
  71.     }
  72.     return n;
  73.     }
  74.  
  75.  
  76.     public boolean isEmpty() {
  77.     return size() == 0;
  78.     }
  79.  
  80.  
  81.     public Enumeration keys() 
  82.     {
  83.     Enumeration[] enums = new Enumeration[1 + tables.length];
  84.     enums[0] = super.keys();
  85.     for(int i = 0; i < tables.length; i++) {
  86.         UIDefaults table = tables[i];
  87.         if (table != null) {
  88.         enums[i + 1] = table.keys();
  89.         }
  90.     }
  91.     return new MultiUIDefaultsEnumerator(enums);
  92.     }
  93.  
  94.  
  95.     public Enumeration elements() 
  96.     {
  97.     Enumeration[] enums = new Enumeration[1 + tables.length];
  98.     enums[0] = super.elements();
  99.     for(int i = 0; i < tables.length; i++) {
  100.         UIDefaults table = tables[i];
  101.         if (table != null) {
  102.         enums[i + 1] = table.elements();
  103.         }
  104.     }
  105.     return new MultiUIDefaultsEnumerator(enums);
  106.     }
  107.  
  108.  
  109.     private static class MultiUIDefaultsEnumerator implements Enumeration
  110.     {
  111.     Enumeration[] enums;
  112.     int n = 0;
  113.  
  114.     MultiUIDefaultsEnumerator(Enumeration[] enums) {
  115.         this.enums = enums;
  116.     }
  117.  
  118.     public boolean hasMoreElements() {
  119.         for(int i = n; i < enums.length; i++) {
  120.         Enumeration e = enums[i];
  121.         if ((e != null) && (e.hasMoreElements())) {
  122.             return true;
  123.         }
  124.         }
  125.         return false;
  126.     }
  127.  
  128.     public Object nextElement() {
  129.         for(; n < enums.length; n++) {
  130.         Enumeration e = enums[n];
  131.         if ((e != null) && (e.hasMoreElements())) {
  132.             return e.nextElement();
  133.         }
  134.         }
  135.         return null;
  136.     }
  137.     }
  138.  
  139.  
  140.     public Object remove(Object key) 
  141.     {
  142.     Object value = super.remove(key);
  143.     if (value != null) {
  144.         return value;
  145.     }
  146.  
  147.     for(int i = 0; i < tables.length; i++) {
  148.         UIDefaults table = tables[i];
  149.         value = (table != null) ? table.remove(key) : null;
  150.         if (value != null) {
  151.         return value;
  152.         }
  153.     }
  154.  
  155.     return null;
  156.     }
  157.  
  158.  
  159.     public void clear() {
  160.     super.clear();
  161.     for(int i = 0; i < tables.length; i++) {
  162.         UIDefaults table = tables[i];
  163.         if (table != null) {
  164.         table.clear();
  165.         }
  166.     }
  167.     }
  168. }
  169.